package com.example.flex.config;

import com.example.flex.BaseDO;
import com.mybatisflex.core.FlexGlobalConfig;
import com.mybatisflex.core.audit.AuditManager;
import com.mybatisflex.spring.boot.MyBatisFlexCustomizer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;

/**
 * mybatis-flex配置类
 */
@Slf4j
@Configuration
public class MybatisFlexConfig implements MyBatisFlexCustomizer {

    @Override
    public void customize(FlexGlobalConfig globalConfig) {
        //开启审计功能
        AuditManager.setAuditEnable(true);
        //设置 SQL 审计收集器
        AuditManager.setMessageCollector(auditMessage -> {
                    Long tookTimeMillis = auditMessage.getElapsedTime();
                    String sql = auditMessage.getFullSql();
                    if (tookTimeMillis > 200) {
                        log.warn("Slow sql exec took {} ms >>> {}", tookTimeMillis, sql);
                    } else {
                        log.info("Flex exec sql took {} ms >>> {}", tookTimeMillis, sql);
                    }
                }
        );
        globalConfig.registerUpdateListener(new BaseDOUpdateListener(), BaseDO.class);
    }


}
